/***
 * Copyright (c) 2010 readyState Software Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package com.tfleo.mealssenger.balloon;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.ignition.core.widgets.RemoteImageView;
import com.tfleo.mealssenger.R;
import com.tfleo.mealssenger.utility.PlaceIconFetcher;

/**
 * A view representing a MapView marker information balloon.
 * 
 * @author Jeff Gilfelt - modified by tfleo
 */
public class BalloonOverlayView<Item extends CustomOverlayItem> extends FrameLayout {

	private LinearLayout vLayout;
	private TextView vTitle;
	private TextView vSnippet;
	private RemoteImageView vImage;

	/**
	 * Create a new BalloonOverlayView.
	 * 
	 * @param context The activity context.
	 * @param balloonBottomOffset The bottom padding to be applied when rendering this view.
	 */
	public BalloonOverlayView(Context context, int balloonBottomOffset) {
		super(context);

		setPadding(10, 0, 10, balloonBottomOffset+35);
		vLayout = new LinearLayout(context);
		vLayout.setVisibility(VISIBLE);

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		View v = inflater.inflate(R.layout.balloon_overlay, vLayout);
		
		vTitle = (TextView) v.findViewById(R.id.title);
		vSnippet = (TextView) v.findViewById(R.id.snippet);
		vImage = (RemoteImageView) v.findViewById(R.id.img);
		
		ImageView close = (ImageView) v.findViewById(R.id.close_btn);
		close.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				vLayout.setVisibility(GONE);
			}
		});

		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		params.gravity = Gravity.NO_GRAVITY;

		addView(vLayout, params);
	}
	
	/**
	 * Sets the view data from a given overlay item.
	 * 
	 * @param item The overlay item containing the relevant view data. 
	 */
	public void setData(Item item) {
		vLayout.setVisibility(VISIBLE);
		if (item.getTitle() != null) {
			vTitle.setVisibility(VISIBLE);
			vTitle.setText(item.getTitle());
		} else {
			vTitle.setVisibility(GONE);
		}
		
		if (item.getSnippet() != null) {
			vSnippet.setVisibility(VISIBLE);
			vSnippet.setText(item.getSnippet());
		} else {
			vSnippet.setVisibility(GONE);
		}
		
		if(item.imgRes != null) {
			vImage.setVisibility(VISIBLE);
			if(item.isUser) {
				vImage.setImageUrl(item.imgRes);
				vImage.loadImage();
			} else {
				vImage.setNoImageDrawable(PlaceIconFetcher.getIcon(item.imgRes));
				//vImage.setImageResource(PlaceIconFetcher.getIcon(item.imgRes));
			}
		} else {
			vImage.setVisibility(GONE);
		}
		
	}

}
